import 'dart:math';

import 'package:abc/model/post_model.dart';
import 'package:abc/view/pagination_list_view.dart';
import 'package:abc/view/video_model.dart';
import 'package:get/get.dart';

class MyLikesController extends GetxController{
  // 当前选中的tab索引
  final tabIndex = 0.obs;
  
  // 视频数据控制器
  late VideoListController videoController;
  
  // 帖子数据控制器
  late PostListController postController;
  
  @override
  void onInit() {
    super.onInit();
    // 初始化控制器
    videoController = VideoListController(tabIndex.value);
    postController = PostListController();
  }
  
  // 切换tab
  void switchTab(int index) {
    tabIndex.value = index;
    print('miki--------=tabIndex=${tabIndex.value}');
    // 更新视频控制器的tabIndex
    if (index != 4) { // 不是帖子tab
      videoController.tabIndex = index;
      // 通知UI更新
      update();
    }
  }
 
}

// 视频数据控制器
class VideoListController extends PaginationListController<VideoModel> {
   int tabIndex;
  
  VideoListController(this.tabIndex);
  
  @override
  Future<List<VideoModel>> loadDatas(int currentPage, int pageSize, bool hasMore) async {
    print('miki=============tabIndex=${tabIndex}');
    // 模拟网络请求延迟
    await Future.delayed(Duration(milliseconds: 1000));

    //0视频 1动漫  
    int currentcontentType = 1;
    if(tabIndex == 0 || tabIndex == 1){
      currentcontentType = 1;
    }else{
      currentcontentType = 0;
    }
    List coverList = ['https://q9.itc.cn/q_70/images03/20250912/952d5470f60d4dd3813f0a23f2824b53.jpegg'
    'https://gd-hbimg.huaban.com/4ce81cb78231b569fcf4e00f798233e070e340e2a1209-dFqFF6_fw658',
    'https://i1.hdslb.com/bfs/archive/3fb37ddc54b8c620cf0b8a2d06e7a4febb322db5.jpg',
    'https://bpic.588ku.com//Templet_origin_pic/05/09/45/d6073024c08b024baace8c277fc32ef3.jpg!/fw/329/quality/90/unsharp/true/compress/true',
    'https://img1.baidu.com/it/u=1074403056,4017061188&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=701',
    'https://gd-filems.dancf.com/gaoding/cms/mcm79j/mcm79j/86421/cf995c09-8657-4473-93a6-6ca45d22160a895481.png',
    'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893'];

    
    // 生成模拟数据
    List<VideoModel> pageItems = [];
    int startIndex = (currentPage - 1) * pageSize;
    
    // 生成当前页的数据，根据不同的tab生成不同的数据
    for (int i = 0; i < pageSize; i++) {
      final index = startIndex + i;
      
      // 创建一个VideoModel实例，根据tabIndex生成不同的数据
      final video = VideoModel(
        id: index,
        isAd: 0,
        resourceId: 'resource_${tabIndex}_$index',
        name: '${_getTabName(tabIndex)} $index',
        coverImage: coverList[Random().nextInt(coverList.length)],
        coverImageY: 'assets/images/default_video_cover.png',
        updateStatus: '1',
        updateStatusText: '更新至${Random().nextInt(100) + 1}话',
        duration: '${(index % 10) + 1}:3${index % 10}',
        description: '${_getTabName(tabIndex)}视频描述 $index',
        previewMvUrl: '',
        mvUrl: '',
        watchNum: (index * 1234) % 100000,
        likeNum: 0,
        realWatchNum: 0,
        realLikeNum: 0,
        commentNum: 0,
        collectNum: 0,
        shareNum: 0,
        isLike: 0,
        tags: ['标签1', '标签2'],
        discount: 0,
        level: Random().nextInt(8) + 1,
        payType: Random().nextInt(4) + 1, // 随机生成1到3之间的数字// 付费类型 1金币 2 会员 3 免费 4 用户等级
        price: 0,
        contentType: currentcontentType, // 使用tabIndex作为contentType
      );
      
      pageItems.add(video);
    }
    
    return pageItems;
  }
  
  // 根据tab索引获取tab名称
  String _getTabName(int index) {
    switch (index) {
      case 0:
        return '动漫';
      case 1:
        return '漫画';
      case 2:
        return '长视频';
      case 3:
        return '短视频';
      case 4:
        return '帖子';
      default:
        return '视频';
    }
  }
}

// 帖子数据控制器
class PostListController extends PaginationListController<Post> {
  get items => null;

  @override
  Future<List<Post>> loadDatas(int currentPage, int pageSize, bool hasMore) async {
    // 模拟网络请求延迟
    await Future.delayed(Duration(milliseconds: 1000));
    
    // 生成模拟数据
    List<Post> pageItems =  [
    // 帖子 1: 无图片
    Post(
      avatarUrl: 'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      username: '空大菜菜',
      time: '9小时前',
      title: '今日三餐',
      content: '今天尝试了一下新的菜式，全家都说好吃！特别推荐给大家...',
      imageUrls: [],
      tags: ['日常', '美食'],
      likes: 852, comments: 125,watch: 100, shares: 36, isFollowed: true,
      status: 0
    ),
    // 帖子 2: 三张图片
    Post(
      avatarUrl: 'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      username: '天空蔚蓝',
      time: '2小时前',
      title: '今日午餐',
      content: '美食美景，人生一大享受！',
      imageUrls: [
        'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
        'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
        'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      ],
      tags: ['日常', '美食'],
      likes: 980, comments: 345,watch: 130, shares: 120, isFollowed: false,
      status: 0
    ),
    // 帖子 3: 两张图片
    Post(
      avatarUrl: 'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      username: '小美酱',
      time: '刚刚',
      title: '今日穿搭',
      content: '新的裙子，新的心情~',
      imageUrls: [
        'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
        'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
       
      ],
      tags: ['日常', '美食'],
      likes: 1200, comments: 450,watch: 3550, shares: 200, isFollowed: false,
      status: 0
    ),
    // 帖子 4: 1张图片
    Post(
      avatarUrl: 'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      username: '广告推广',
      time: '推广',
      title: '汽车广告',
      content: '', // 广告内容通常在图片中
      imageUrls: ['https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893'], // 模拟横幅广告图
      tags: ['日常', '美食'],
      likes: 0, comments: 0,watch: 10, shares: 0, isFollowed: true,
      status: 0
    ),

    // 帖子 5: 4张图片 
    Post(
      avatarUrl: 'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      username: '广告推广',
      time: '推广',
      title: '汽车广告',
      content: '', // 广告内容通常在图片中
      imageUrls: ['https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893',
      'https://img1.baidu.com/it/u=877618338,2738144752&fm=253&app=138&f=JPEG?w=608&h=893'], // 模拟横幅广告图
      tags: ['日常', '美食'],
      likes: 0, comments: 0,watch: 17, shares: 0, isFollowed: true,
      status: 0
    ),
  ];
    
  
    
    return pageItems;
  }
}